package Demo1;

import java.util.Arrays;
import java.util.Objects;

class Student implements Comparable<Student>{
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student student1=new Student(20,"dingding");
        Student student2=new Student(20,"dingding");
        System.out.println(student1.equals(student2));

    }
    public static void mysort(Comparable[] comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp=comparables[j];
                    comparables[j]=comparables[j+1];
                    comparables[j+1]=tmp;
                }
            }
        }
    }
    public static void main1(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student(17,"niuniu");
        students[1]=new Student(20,"dingding");
        students[2]=new Student(16,"hanghang");
        System.out.println("排序前"+ Arrays.toString(students));
        NameComparator nameComparator=new NameComparator();
        AgeComparator ageComparator=new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后"+ Arrays.toString(students));

    }
}
